package com.ruiysoft.mapper.order;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruiysoft.controller.sys.dto.OrderRelationCommissionDto;
import com.ruiysoft.entity.order.OrderRelationCommission;
import com.ruiysoft.entity.sys.SysShopPersonnel;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * <p>
 *  Mapper 接口
 * </p>
 *
 * @author huazengguang
 * @since 2019-10-13
 */
public interface OrderRelationCommissionMapper extends BaseMapper<OrderRelationCommission> {

    @Select("SELECT shopPersonnel.`real_name` realName,shopPersonnel.mobile mobileNo,shopPersonnel.`id`,commission.orderAmount,commission.amount,commission.orderCount,distribution.name distributionName,\n" +
            "  distribution.id distributionId FROM (SELECT userId,SUM(IFNULL(orderAmount, 0)) orderAmount,SUM(IFNULL(amount, 0)) amount, SUM(1) orderCount,\n" +
            "    shopId FROM order_relation_commission commission GROUP BY userId) commission LEFT JOIN sys_shop_personnel shopPersonnel ON commission.userId = shopPersonnel.`id` \n" +
            "  LEFT JOIN sys_shop_personnel_relation relation ON shopPersonnel.id = relation.personnel_id LEFT JOIN sys_shop_distribution distribution \n" +
            "    ON relation.distributionId = distribution.id WHERE commission.shopid = #{shopId } AND relation.shop_id=#{shopId } ")
    List<OrderRelationCommissionDto> getRelationCommissionList(Page<OrderRelationCommissionDto> page,@Param("shopId") Long shopId);

    @Select("SELECT commission.*,personnel.`nickname` buyerName FROM order_relation_commission commission LEFT JOIN sys_shop_personnel personnel ON commission.`buyerId`=personnel.`id` WHERE commission.userId=#{userId } AND commission.shopId=#{shopId } order by commission.create_time desc")
    List<OrderRelationCommission> getRelationCommissionByUserId(Page<OrderRelationCommission> page, @Param("shopId")Long shopId, @Param("userId")Long userId);

    @Select("SELECT commission.*,personnel.`nickname` buyerName FROM order_relation_commission commission LEFT JOIN sys_shop_personnel personnel ON commission.`buyerId`=personnel.`id` WHERE commission.buyerId=#{userId } AND commission.userId=#{id } AND commission.shopId=#{shopId } order by commission.create_time desc")
    List<OrderRelationCommission> getRelationCommissionByBuyerId(Page<OrderRelationCommission> page, @Param("shopId")Long shopId, @Param("userId")Long userId,@Param("id")Long id);

    @Select("SELECT commission.userId,commission.buyerId,sum(ifnull(commission.orderAmount,0)) orderAmount,sum(ifnull(commission.amount,0)) amount,personnel.`nickname` buyerName FROM order_relation_commission commission LEFT JOIN sys_shop_personnel personnel ON commission.`buyerId`=personnel.`id` WHERE commission.userId=#{userId } AND commission.shopId=#{shopId } group by commission.userId order by commission.create_time desc")
    List<OrderRelationCommission> getRelationCommissionDisByUserId(Page<OrderRelationCommission> page, Long shopId, Long userId);

    @Select("SELECT commission.buyerId userId,personnle.`nickname` realName,personnle.head_img_url headImgUrl,SUM(IFNULL(orderAmount, 0)) orderAmount,SUM(IFNULL(amount, 0)) amount, COUNT(1) orderCount, shopId ,commission.`relationLevel`\n" +
            "             FROM  order_relation_commission commission LEFT JOIN sys_shop_personnel personnle ON commission.`buyerId`=personnle.`id` \n" +
            "             WHERE commission.`userId`=#{userId } AND commission.`shopId`=#{shopId } GROUP BY buyerId ORDER BY SUM(IFNULL(orderAmount, 0)) DESC")
    List<OrderRelationCommissionDto> getCurrentDistributionerList(Page<OrderRelationCommissionDto> page, @Param("shopId")Long shopId, @Param("userId")Long userId);

//    @Update("update order_relation_commission set settleFlag=1 ")
//    int settleCommissionByIds(List<String> ids);
}
